<script setup>
import { computed } from 'vue';
import { useTodosStore } from '../store';

const todos = useTodosStore();
console.log('todos -----> ', todos);
// subscribe订阅了todos的变化，每当 todos 变化就执行回调函数，等同于 watch
// 注意，todos.$subscribe是在组件内调用的，不要写到 todos.js 里面
todos.$subscribe(() => {
  localStorage.setItem('todos', JSON.stringify(todos.list));
});

const isAll = computed(() => {
  return todos.list.length > 0 && todos.list.every((v) => v.done);
});
</script>

<template>
  <section class="main">
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      :checked="isAll"
      @change="todos.changeAll($event.target.checked)"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li
        :class="{ completed: item.done }"
        v-for="item in todos.showList"
        :key="item.id"
      >
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            :checked="item.done"
            @change="todos.changeDone(item.id)"
          />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="todos.del(item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped></style>
